//
//  shoucangView.m
//  HYHoney
//
//  Created by dllo on 16/3/1.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "shoucangView.h"
#import "shoucang.h"
#import "DataBaseHandle.h"
#import "ModelForPush.h"
#import "UIImageView+WebCache.h"
#import "PushForPush.h"
#import "DKNightVersion.h"

@interface shoucangView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic,retain)UITableView *tableView;
@property (nonatomic, retain) NSMutableArray *array;

@end

@implementation shoucangView


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"心水收藏";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor colorWithRed:1 green:1 blue:1 alpha:1], NSForegroundColorAttributeName,  nil]];
    
    [self createTableView];
}



- (void)createTableView{
    
    self.tableView = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStyleGrouped];
    
    self.tableView.dk_backgroundColorPicker = DKColorWithRGB(0xFFFAFA, 0x27408B);
    [self.view addSubview:self.tableView];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    [self.tableView registerClass:[shoucang class] forCellReuseIdentifier:@"pool"];
}



- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.array = [[DataBaseHandle shareDataBaseHandle] select].mutableCopy;
    [self.tableView reloadData];
        
}



- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    shoucang *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];
    
    ModelForPush *model = [self.array objectAtIndex:indexPath.row];
    cell.textLabel.dk_textColorPicker = DKColorWithRGB(0x000000, 0xefffff);
    
    [cell passModel:model];
    
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.array.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 120;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PushForPush *url = [[PushForPush alloc]init];
    
    ModelForPush *model = [self.array objectAtIndex:indexPath.row];
    [url passModelForPush:model];
    
    [self.navigationController pushViewController:url animated:YES];
}



#pragma mark - 编辑删除
- (void)setEditing:(BOOL)editing animated:(BOOL)animated{
    
    [super setEditing:editing animated:animated];
    
    [self.tableView setEditing:editing animated:animated];
    
}



- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return YES;
}



- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (UITableViewCellEditingStyleDelete == editingStyle) {
        
        ModelForPush *model = [self.array objectAtIndex:indexPath.row];
        //从数据源删除
        [self.array removeObjectAtIndex:indexPath.row];
        //从 tableView 删除
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [[DataBaseHandle shareDataBaseHandle]deleteWithModel:model];
        
    }

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
